package com.tempo.widget;
/**
 * created by tempo on 2015/11/13.
 * 自动换行控件
 * i:可以设置行数
 * ii:可以设置子View的宽高比
 */

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class AutoLineLayout extends ViewGroup {
    /**
     * 每行的个数
     */
    private int numClunms = 2;
    /**
     * 列间距
     */
    private int clumnScap=1;
    /**
     * 行间距
     */
    private int rowScap=1;
    /**
     * 宽高比
     */
    private float aspectRatio=1;

    private int itemWidth;
    private int itemHeight;
    private AutoAdapter mAutoAdapter;
    public AutoLineLayout(Context context) {
        this(context, null);
    }

    public AutoLineLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AutoLineLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
       TypedArray typedArray= context.obtainStyledAttributes(attrs, R.styleable.AutoLineLayout);
        numClunms = (int) typedArray.getInteger(R.styleable.AutoLineLayout_numClumns, numClunms);
        clumnScap= (int) typedArray.getDimension(R.styleable.AutoLineLayout_clumnScap, clumnScap);
        rowScap= (int) typedArray.getDimension(R.styleable.AutoLineLayout_rowScap,rowScap);
        aspectRatio=typedArray.getFloat(R.styleable.AutoLineLayout_aspectRatio,aspectRatio);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int bWidth = MeasureSpec.getSize(widthMeasureSpec);
        int bHeight = MeasureSpec.getSize(heightMeasureSpec);
        int bWidthMode = MeasureSpec.getMode(widthMeasureSpec);
        int bHeightMode = MeasureSpec.getMode(heightMeasureSpec);


         itemWidth=(bWidth-getPaddingRight()-getPaddingLeft()-(numClunms-1)*clumnScap)/numClunms;
         itemHeight= (int) (itemWidth/aspectRatio);

        int childWidthMeasureSpec=MeasureSpec.makeMeasureSpec(itemWidth,MeasureSpec.EXACTLY);
        int childHeightMeasureSepc = MeasureSpec.makeMeasureSpec(itemHeight, MeasureSpec.EXACTLY);

        int childCount = getChildCount();
        for(int i=0;i<childCount;i++){
            View v=getChildAt(i);
            v.measure(childWidthMeasureSpec,childHeightMeasureSepc);
        }
        int rows=childCount/numClunms+childCount%numClunms;
        int aHeight=getPaddingTop()+getPaddingBottom()+(rows-1)*rowScap+rows*itemHeight;
        int aWidthMeasureSpec=MeasureSpec.makeMeasureSpec(bWidth,MeasureSpec.EXACTLY);
        int aHeightMeasureSpec=MeasureSpec.makeMeasureSpec(aHeight,MeasureSpec.EXACTLY);
        setMeasuredDimension(aWidthMeasureSpec,aHeightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int itemCount=getChildCount();
        for(int i=0;i<itemCount;i++) {
            View child = getChildAt(i);
            int row=i/numClunms;
            int index=i%numClunms;
            int left=getPaddingLeft()+index*(clumnScap+itemWidth);
            int right=left+itemWidth;
            int top=getPaddingTop()+row*(rowScap+itemHeight);
            int bottom=top+itemHeight;
            child.layout(left,top,right,bottom);
        }
    }
    public abstract static class AutoAdapter{
        private AutoLineLayout mAutoLineLayout;
        protected abstract int getCount();
        protected abstract Object getData(int position);
        protected abstract View getView(View parent,int position);
        protected abstract void resetData(View child,int position);
        public void notifyDataChaged(){
               if(mAutoLineLayout!=null){
                   mAutoLineLayout.notifyDataChanged();
               }
        }

        public void setParentView(AutoLineLayout autoLineLayout) {
            this.mAutoLineLayout=autoLineLayout;
        }
    }
    public void setAdapter(AutoAdapter adapter){
        mAutoAdapter=adapter;
        mAutoAdapter.setParentView(this);
        notifyDataChanged();
    }

    private void notifyDataChanged() {
        if(mAutoAdapter!=null) {
            if(getChildCount()!=mAutoAdapter.getCount()){
                reloadChildsView();
            }
            for(int i=0;i<getChildCount();i++) {
                View child = getChildAt(i);
                mAutoAdapter.resetData(child,i);
            }
        }
    }
    private void reloadChildsView(){
        removeAllViews();
        if(mAutoAdapter!=null){
            int childCount = mAutoAdapter.getCount();
            for (int i = 0; i < childCount; i++) {
                View child = mAutoAdapter.getView(this, i);
                addView(child);
            }
        }
    }
    @Override
    protected void onFinishInflate() {
        if (this.getChildCount() != 0) {
            throw new IllegalStateException("please add child by adapter,using xml is not allowed here !");
        }
    }
}
